package com.zd.zjfx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zd.zjfx.util.Httptest;
import com.zd.zjfx.util.Lq_Security;
import com.zd.zjfx.util.ZipBase64;


@Controller
@RequestMapping(value = "/jmltest")
public class TestController {
	@Value("${sandbox.appKey}")
	private String appKey;

	@Value("${sandbox.jrdwptbh}")
	private String jrdwptbh;

	@Value("${sandbox.sydwptbh}")
	private String sydwptbh;

	@ResponseBody
	@RequestMapping(value = "/sandbox")
	public HashMap<String, String> sandbox(String path, String code, String headers) throws Exception {
		HashMap<String, String> result=new HashMap<String, String>();
		System.out.println(new Date().toString());
		String dec="";
		String decode="";
		//解析headers
		Map<String, Object> headersMap = new HashMap<>();
		if (StringUtils.isNotEmpty(headers)) {
			String[] headerArr = headers.split("\n");
			for (int i = 0; i < headerArr.length; i++) {
				String[] paramArr = headerArr[i].split(": ");
				headersMap.put(paramArr[0], paramArr[1]);
			}
		}
		String basevalue=Httptest.shaxiang(path, Lq_Security.encryptEcb(appKey,code),jrdwptbh,sydwptbh,headersMap);
		try {
			String value=basevalue.split("Data\\\\\\\":\\\\\\\"")[1];
			dec=Lq_Security.decryptEcb(appKey, value.split("\\\\\\\"}}")[0]);
			if (dec.indexOf("\"H4s")>0) {
				decode=dec.split("\"H4s")[1].split("\"")[0];
				decode=ZipBase64.unGZipString("H4s" + decode);
			} else if (dec.indexOf("\"UEs")>0) {
				decode=dec.split("\"UEs")[1].split("\"")[0];
				decode=ZipBase64.unZipString("UEs" + decode);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.put("base", basevalue);
		result.put("dec", dec);
		result.put("decode", decode);
		System.out.println(new Date().toString());
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/ceshi")
	public HashMap<String, String> getDm(String path,String code) throws Exception {
		HashMap<String, String> result=new HashMap<String, String>();
		System.out.println(new Date().toString());
		String dec="";
		String decode="";
		String basevalue=Httptest.interfaceUntil(path, Lq_Security.encryptEcb(appKey,code),jrdwptbh,sydwptbh);
		try {
			String value=basevalue.split("Data\\\\\\\":\\\\\\\"")[1];
			dec=Lq_Security.decryptEcb(appKey, value.split("\\\\\\\"}}")[0]);
			if (dec.indexOf("\"H4s")>0) {
				decode=dec.split("\"H4s")[1].split("\"")[0];
				decode=ZipBase64.unGZipString("H4s" + decode);
			} else if (dec.indexOf("\"UEs")>0) {
				decode=dec.split("\"UEs")[1].split("\"")[0];
				decode=ZipBase64.unZipString("UEs" + decode);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.put("base", basevalue);
		result.put("dec", dec);
		result.put("decode", decode);
		System.out.println(new Date().toString());
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/zhenghi")
	public HashMap<String, String> zhenghi(String path,String code) throws Exception {
		HashMap<String, String> result=new HashMap<String, String>();
		System.out.println(new Date().toString());
		String dec="";
		String decode="";
		String basevalue=Httptest.interfaceUntil(path, Lq_Security.encryptEcb(appKey,code),jrdwptbh,sydwptbh);
		try {
			String value=basevalue.split("Data\\\\\\\":\\\\\\\"")[1];
			dec=Lq_Security.decryptEcb(appKey, value.split("\\\\\\\"}}")[0]);
			if (dec.indexOf("\"H4s")>0) {
				decode=dec.split("\"H4s")[1].split("\"")[0];
				decode=ZipBase64.unGZipString("H4s" + decode);
			} else if (dec.indexOf("\"UEs")>0) {
				decode=dec.split("\"UEs")[1].split("\"")[0];
				decode=ZipBase64.unZipString("UEs" + decode);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.put("base", basevalue);
		result.put("dec", dec);
		result.put("decode", decode);
		System.out.println(new Date().toString());
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/test")
	public boolean getTest() throws Exception {
		System.out.println(new Date().toString());
		Httptest.interfaceUntil("https://lqpt.chinatax.gov.cn:8443/access/sandbox/v2/invoke/202007/CXNSRJBXX/",
				Lq_Security.encryptEcb("9904033e1837094cfe11772fc2d90011", "{\"nsrsbh\":\"911305005809845195\"}"),"","");
		System.out.println(new Date().toString());
		return true;
	}
}
